package com.zhsm.fuxi.mvp.ui.dialog

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zhsm.fuxi.R
import com.zhsm.fuxi.base.BaseDialog
import kotlinx.android.synthetic.main.dialog_check_version.*
import org.jetbrains.anko.sdk25.coroutines.onClick

/**
 * Created by GuZhC on 2018/11/20.
 */
class UpdateVercionDialog(context: Context) : BaseDialog(context) {


    override fun getLayoutId(): Int {
        return R.layout.dialog_check_version
    }

    override fun getGravity(): Int {
        return BaseDialog.GRAVITY_CENTER
    }

    override fun isCanceledOnTouchOutside(): Boolean {
        return true
    }

    override fun initView() {
        super.initView()
        btnUpdateYes.onClick {
            mClickBtn?.sureBtn()
        }
        btnUpdateNo.onClick { mClickBtn?.cancelBtn() }
        dialogRoot.onClick { dismiss() }
    }

    interface onClickBtn {
        //保存
        fun sureBtn()

        fun cancelBtn()
    }

    private var mClickBtn: onClickBtn? = null

    fun setOnClickAndRes(version: String, force: Boolean, descriptions: List<String?>?, mClickbtn: onClickBtn) {
        this.mClickBtn = mClickbtn
        tvVersion.text = version
        if (force)
            btnUpdateNo.text = "退出应用"
        else btnUpdateNo.text = "暂不更新"
        if (descriptions !== null && descriptions.isNotEmpty()) {
            recyclerContent.visibility = View.VISIBLE
            recyclerContent.layoutManager = LinearLayoutManager(context)
            recyclerContent.adapter = object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_update_version, descriptions) {
                override fun convert(helper: BaseViewHolder?, item: String?) {
                    helper?.setText(R.id.tv_upversion_info, item)
                }
            }
        } else recyclerContent.visibility = View.GONE
    }

}